package services

import (
	"gin-app/app/common/request"
	"gin-app/app/models"
	"gin-app/global"
)

type personInChargeService struct {
}

var PersonInChargeService = new(personInChargeService)

// 新建零售部负责人
func (n personInChargeService) CreateRetailPersonInCharge(params request.CreateRetailPersonInCharge) (err error, retailPersonInCharge *models.RetailPersonInCharge) {
	//chapter = models.NewChapter(params.NovelID, params.Name, params.Content)
	retailPersonInCharge = models.NewRetailPersonInCharge(params.Name, params.Gender, params.Phone, params.Area, params.Record)

	err = global.App.DB.Create(retailPersonInCharge).Error
	return
}

// 删除零售负责人
func (n personInChargeService) DeleteRetailPersonInCharge(params request.DeleteRetailPersonInCharge) (err error) {
	//err = global.App.DB.Find().Error
	retailPersonInCharge := &models.RetailPersonInCharge{}
	err = global.App.DB.Take(retailPersonInCharge, params.ID).Error
	if err != nil {
		return
	}
	err = global.App.DB.Delete(retailPersonInCharge).Error
	return
}

// 获取零售负责人列表
func (n personInChargeService) GetRetailPersonInChargeList() (err error, retailPersonInCharges []models.RetailPersonInCharge) {
	err = global.App.DB.Find(&retailPersonInCharges).Error
	return
}

// 修改零售负责人
func (n personInChargeService) EditorRetailPersonInCharge(params request.EditorRetailPersonInCharge) (err error, retailPersonInCharge models.RetailPersonInCharge) {
	//err = global.App.DB.Find().Error
	err = global.App.DB.Take(&retailPersonInCharge).Where("id=?", params.ID).Error
	if err != nil {
		return
	}
	retailPersonInCharge.Name = params.Name
	retailPersonInCharge.Gender = params.Gender
	retailPersonInCharge.Phone = params.Phone
	retailPersonInCharge.Area = params.Area
	retailPersonInCharge.Record = params.Record
	err = global.App.DB.Save(&retailPersonInCharge).Error
	return
}

// 新建水源部负责人
func (n personInChargeService) CreateWaterSourcePersonInCharge(params request.CreateWaterSourcePersonInCharge) (err error, waterSourcePersonInCharge *models.WaterSourcePersonInCharge) {
	//chapter = models.NewChapter(params.NovelID, params.Name, params.Content)

	waterSourcePersonInCharge = models.NewWaterSourcePersonInCharge(params.Name, params.Gender, params.Phone, params.Area, params.Record)
	err = global.App.DB.Create(waterSourcePersonInCharge).Error
	return
}

// 删除水源负责人
func (n personInChargeService) DeleteWaterSourcePersonInCharge(params request.DeleteWaterSourcePersonInCharge) (err error) {
	//err = global.App.DB.Find().Error
	waterSourcePersonInCharge := &models.WaterSourcePersonInCharge{}
	err = global.App.DB.Take(waterSourcePersonInCharge, params.ID).Error
	if err != nil {
		return
	}
	err = global.App.DB.Delete(waterSourcePersonInCharge).Error
	return
}

// 获取水源负责人列表
func (n personInChargeService) GetWaterSourcePersonInChargeList() (err error, waterSourcePersonInCharges []models.WaterSourcePersonInCharge) {
	err = global.App.DB.Find(&waterSourcePersonInCharges).Error
	return
}

// 修改水源负责人
func (n personInChargeService) EditorWaterSourcePersonInCharge(params request.EditorWaterSourcePersonInCharge) (err error, retailPersonInCharge models.WaterSourcePersonInCharge) {
	//err = global.App.DB.Find().Error
	err = global.App.DB.Take(&retailPersonInCharge).Where("id=?", params.ID).Error
	if err != nil {
		return
	}
	retailPersonInCharge.Name = params.Name
	retailPersonInCharge.Gender = params.Gender
	retailPersonInCharge.Phone = params.Phone
	retailPersonInCharge.Area = params.Area
	retailPersonInCharge.Record = params.Record
	err = global.App.DB.Save(&retailPersonInCharge).Error
	return
}
